package com.evil.common.application.enums.queue;

import com.evil.common.core.enums.BaseEnum;
import com.evil.common.family.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Optional;

/**
 * 队列状态
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum QueueStatusEnum {

    /**
     * 队列状态（0-准备中，1-已完成，2-失败）
     */
    PREPARING(0, "准备中"),
    COMPLETE(1, "已完成"),
    FAILED(2, "失败"),
    ;

    @Getter
    private final int id;
    @Getter
    private final String name;

    public static Optional<QueueStatusEnum> findById(Integer id) {
        if (null == id) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static QueueStatusEnum isExistById(Integer id) {
        return QueueStatusEnum.findById(id).orElseThrow(QueueStatusEnum::getBusinessException);
    }

    public static BusinessException getBusinessException() {
        return new BusinessException(BaseEnum.ERROR_PARAMETER, "队列状态参数错误");
    }
}
